﻿$(function () {
    $("#btnSubmitQA").live("click", btnSubmitQAClick);
    $("#btnSearch").live("click", btnSearchClick);
    
    var info = $("#hiddenInfo").val();
    if (info == "true") {
        //location.reload();
        alert("提交成功");
    } else
        if ($.trim(info) != "") {
            alert(info);
        }

    function btnSubmitQAClick() {
        var content = $("#content").val();
        var contactWay = $("#contactWay").val();
        if ($.trim(content) == "") {
            alert("问题描述不得为空！");
            $("#content").focus();
            return;
        }

        if ($.trim(contactWay) == "") {
            alert("联系方式不得为空！");
            $("#contactWay").focus();
            return;
        }

        $("#QAAddForm").submit();
    };

    var Page_Size = 5;
    GetflowLogPage({ page: 1, pageSize: Page_Size });
    
    function btnSearchClick()
    {
        var keyWord = $("#txtKeyWord").val();
        GetflowLogPage({ page: 1, pageSize: Page_Size, keyWord: $.trim(keyWord) });
    }
    function GetflowLogPage(f) {
        //分页控件   
        var count = GetDataCount(f);
        $("#pp").pagination(count, {
            link_to: "javascript:void(0)",
            num_edge_entries: 2, //边缘页数
            num_display_entries: 7, //主体页数
            callback: onSelectPageFun,
            items_per_page: Page_Size, //每页显示项
            prev_text: "&laquo;",
            next_text: "&raquo;"
        });

        //响应事件
        function onSelectPageFun(page) {
            f.page = page + 1;
            var url = "../QA/GetQAList";
            $.ajax({
                url: url,
                data: f,
                dataType: "json",
                beforeSend: function () {

                },
                type: "post",
                success: function (result) {
                    renderFun(result);//响应事件
                },
                async: true,
                complete: function () {
                }
            });
        }
        //END
    };

    //数据渲染
    function renderFun(result) {
        var $container = $("#divQAContent");
        $container.empty();
        var list = result;
        var source = $("#QA-template").html();
        var template = Handlebars.compile(source);
        var html = template({ qas: list });
        $container.append(html);
    };
    function GetDataCount(f) {
        var count = 0;
        $.ajax({
            url: "../QA/GetQACount",
            data: f,
            success: function (res) {
                count = res;
            },
            async: false
        });
        return count;
    }

});